package com.haste.controller;

import com.haste.pojo.CommonResult;
import com.haste.pojo.Payment;
import com.haste.service.PaymentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.ZonedDateTime;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@RestController
@Slf4j
public class PaymentController {
    @Resource
    private PaymentService paymentService;
    @Resource
    private DiscoveryClient discoveryClient;

    /**
     *@Author:haste
     *@Description:保存一条记录
     *@Date: 2021/2/5 16:54
     **/
    @PostMapping("/payment/save")
    public CommonResult save(@RequestBody Payment payment){
        Integer result = paymentService.save(payment);
        if (result > 0){
            return new CommonResult(200,"插入成功",result);
        }else {
            return new CommonResult(444,"插入失败",null);
        }
    }

    /**
     *@Author:haste
     *@Description:根据id查询
     *@Date: 2021/2/5 16:55
     **/
    @GetMapping("/payment/detail/{id}")
    public CommonResult<Payment> detail(@PathVariable("id") Long id){
        Payment payment = paymentService.detail(id);
        System.out.println(ZonedDateTime.now());
        if (payment != null){
            return new CommonResult(200,"查询成功",payment);
        }else {
            return new CommonResult(444,"查询失败,没有"+id+"这个id的记录",null);
        }
    }

    /**
     *@Author:haste
     *@Description:查询所有
     *@Date: 2021/2/5 16:55
     **/
    @GetMapping("/payment/list")
    public CommonResult list(){
        List<Payment> paymentList = paymentService.list();
        if (paymentList != null){
            return new CommonResult(200,"查询成功",paymentList);
        }else {
            return new CommonResult(444,"查询失败",null);
        }
    }

    @GetMapping("/payment/discovery")
    public Object discovery (){
        List<String> services = discoveryClient.getServices();
        for (String service : services) {
            log.info("service:"+service);
        }
        List<ServiceInstance> payment8001 = discoveryClient.getInstances("PAYMENT8001");
        for (ServiceInstance serviceInstance : payment8001) {
            log.info("serviceInstanceID:"+serviceInstance.getInstanceId()+
                    "\t"+"serviceInstancePort:"+serviceInstance.getPort()+
                    "\t"+"serviceInstanceURI:"+serviceInstance.getUri());
        }
        return this.discoveryClient;
    }

    @GetMapping("/payment/lb")
    public String getLB(){
        return "payment:"+ UUID.randomUUID().toString();
    }

    @GetMapping("/payment/timeout")
    public String paymentFeignTimeout(){
        try {
            TimeUnit.SECONDS.sleep(3);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return UUID.randomUUID().toString();
    }
}
